package com.xuanyuanxinyu.func.network.internal

import android.text.TextUtils
import android.util.Log
import com.xuanyuanxinyu.utils.log.AppLog
import com.xuanyuanxinyu.utils.log.AppLog.logE

import okhttp3.Dns
import okhttp3.OkHttpClient
import java.net.Inet4Address
import java.net.InetAddress
import java.net.UnknownHostException


/**
 * @author tanzh
 */
class MyDns : Dns {
    @Synchronized
    @Throws(UnknownHostException::class)
    override fun lookup(hostname : String) : List<InetAddress> {
        //        if (TextUtils.isEmpty(hostname)) {
        //            return SYSTEM.lookup(hostname);
        //        }
        logE(MyDns::class.java, AppLog.GROUP_NETWORK,"lookup()  hostname = $hostname")
        //        if (null == inetAddressList || inetAddressList.isEmpty()) {
        //            throw new UnknownHostException("unable to find dns host=" + s);
        //        } else {
        //            for (InetAddress item : inetAddressList) {
        //                AppLog.INSTANCE.logE(MyDns.class, AppLog.GROUP_DNS, "lookup()  size = " + inetAddressList.size() + "  " + item.getHostAddress());
        //            }
        //        }
        var mIpString = ""
        //根据域名获取IP
        val ips = InetAddress.getAllByName(hostname)
        for (inetAddress in ips) {
            mIpString = inetAddress.hostAddress
            logE(MyDns::class.java,AppLog.GROUP_NETWORK,"lookup()222 ------- mIpString = $mIpString")
        }
        val listtemp = InetAddress.getAllByName(hostname)
        if (listtemp != null) {
            for (item in listtemp) {
                logE(MyDns::class.java,AppLog.GROUP_NETWORK,"lookup()222 ------- tempAdress = $item")
            }
        }
        val inetAddressList : MutableList<InetAddress> = ArrayList()
        if (!TextUtils.isEmpty(hostname)) {
            val list = InetAddress.getAllByName(hostname)
            if (list != null) {
                for (item in list) {
                    if (item is Inet4Address) {
                        inetAddressList.add(0,item)
                    } else {
                        inetAddressList.add(item)
                    }
                }
                for (item in inetAddressList) {
                    logE(MyDns::class.java,AppLog.GROUP_NETWORK,"lookup()222  size = " + item + "  " + item.hostAddress)
                }
            }
            return inetAddressList
        } else {
            return Dns.SYSTEM.lookup(hostname)
        }
    }
    
    
    /**
     *
     * 1. 设置公共DNS服务器或自定义服务器
     * 2. 同时可以通过解析耗时,来测量DNS解析性能
     * 3. 自定义DNS解析器时，注意UnknownHostException的处理，确保网络请求的健壮性
     * 4. DNS解析可能会被网络运营商劫持，使用公共DNS服务器可以在一定程度上避免这种情况
     * 5. 实际使用中还要考虑dns解析的缓存策略，避免频繁的解析导致的性能问题
     *
     */
    @Throws(UnknownHostException::class)
    fun setCustomDNS(build : OkHttpClient.Builder) {
        build.dns(object : Dns {
            override fun lookup(hostname : String) : List<InetAddress> {
                // 这里以Google的公共DNS服务器为例
                System.setProperty("sun.net.spi.nameservice.nameservers","8.8.8.8")
                // 使用自定义的DNS服务器地址进行解析
                System.setProperty("sun.net.spi.nameservice.provider.1","dns,sun")
                val startTime : Long = System.currentTimeMillis()
                val addresses = InetAddress.getAllByName(hostname).asList()
                val endTime : Long = System.currentTimeMillis()
                Log.d("DNS Lookup","Hostname: " + hostname + ", Time taken: " + (endTime - startTime) + "ms")
                return addresses
            }
        }).build()
    }
    
    companion object {
        const val TAG : String = "MyDns"
    }
}
